package com.juma.cartridge.retryque;

import java.util.Date;

import lombok.Data;

@Data
public class RetryTask {

	private Long id;
	private String servId;
	private String bizCode;
	private transient String bizData;
	private transient byte[] bizBin;
	private String bizFlag;
	private String detail;
	private long lease;
	private String scanner;
	private TaskStatus status = TaskStatus.RETRY;
	private long round;
	private int retries;
	private Date createTime;
	private Date updateTime;
	private boolean refresh;
	
	public void setDetail(String detail) {		
		this.detail = detail;
		this.refresh = true;
	}
	
	public void setBizFlag(String bizFlag) {
		this.bizFlag = bizFlag;
		this.refresh = true;
	}
	
	public void setBizData(String bizData) {
		this.bizData = bizData;
		this.refresh = true;
	}
	
	public void setStatus(TaskStatus status) {
	    if(this.status!=status) {
	        this.status = status;
	        this.refresh = true;    
	    }
	}
	
	/**
	 * 任务是否非锁定状态
	 * @return
	 */
	public boolean isLockFreed() {
	    return lease<System.currentTimeMillis();
	}
	
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		RetryTask other = (RetryTask) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

}
